import {
	baseUrl,
	request
} from "./url"



const userInfo = uni.getStorageSync('userInfo');
export function wxAuth() {


	if (token || userInfo) {
		return true
	} else {
		uni.showModal({
			title: '您还未授权',
			content: '请先授权在进行操作',
			confirmText: '立即授权',
			cancelText: '暂不授权',
			success: function(res) {
				if (res.confirm) {
					wx.getUserProfile({
						desc: '用于登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							uni.setStorage({
								key: 'userInfo',
								data: res.userInfo,
							});
							uni.switchTab({
								url: '../about/about'
							});
						}
					})
					console.log('用户点击确定');
				} else if (res.cancel) {
					uni.reLaunch({
						url: `/pages/index/login`
					});
					console.log('用户点击取消');
				}
			}
		});
		return false
	}

}
//登录post请求
export function LoginPostAction(url, data) {

	return request({
		url,
		data,
		method: "post",

	});
}
// get
export function getAction(url, data, tk) {
	const token = tk || uni.getStorageSync('token');
	return request({
		url,
		data,
		method: "get",
		header: {
			"Authorization": 'Bearer ' + token
		},
	});
}
//post
//doubles 1是用来区分单参数 2多参数
export function postAction(url, data, doubles = 2) {
	const token = uni.getStorageSync('token');

	return request({
		url,
		data,
		method: "post",
		header: {
			"Content-Type": doubles == 1 ? "application/x-www-form-urlencoded;charset=utf-8" : "application/json",
			"Authorization": 'Bearer ' + token
		},
	});
}
//put
export function putAction(url, data, doubles = 2) {
	const token = uni.getStorageSync('token');

	return request({
		url,
		data,
		method: "put",
		header: {
			"Authorization": 'Bearer ' + token,
			"Content-Type": doubles == 1 ? "application/x-www-form-urlencoded;charset=utf-8" : "application/json",
		},
	});
}
//delete
export function deleteAction(url, data) {
	let baseDefaultOpts = {
		url: baseUrl + url,
		data,
		method: "delete",
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(baseDefaultOpts).then(
			(res) => {
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
}



